clear; clc;
[device, props] = ziCreateAPISession('dev3571', 6);
ziDAQ('sync');

h = ziDAQ('sweep');
ziDAQ('set', h, 'sweep/device', 'dev3571');



ziDAQ('set', h, 'sweep/xmapping', 1); % sweep mode: 0 = linear; 1 = logarithmic
ziDAQ('set', h, 'sweep/scan', 0); %  int     Scan type;   0 = sequential; 1 = binary; 2 = bidirectional;  3 = reverse
ziDAQ('set', h, 'sweep/endless', 0);  % Endless sweeping (default 0) 0 = Use loopcount value; 1 = Endless sweeping enabled, ignore loopcount
ziDAQ('set', h, 'sweep/gridnode', 'oscs/0/freq');

ziDAQ('set', h, 'sweep/start', 1e3);
ziDAQ('set', h, 'sweep/stop', 500e3);
ziDAQ('set', h, 'sweep/loopcount', 1); % int     Number of sweep loops (default 1)
ziDAQ('set', h, 'sweep/samplecount', 100); % int     Number of samples per sweep

ziDAQ('set', h, 'sweep/settling/time', 0);% double  Settling time before measurement is performed, in [s]
ziDAQ('set', h, 'sweep/settling/tc', 0);  % double  Settling precision: 5 ~ low precision; 15 ~ medium precision; 50 ~ high precision
ziDAQ('set', h, 'sweep/settling/inaccuracy', 10e-3);  % double     [1e-13, 0.1]

ziDAQ('set', h, 'sweep/bandwidthcontrol', 0); %int Sets the bandwidth control mode (default 2): 0 = Manual (user sets bandwidth and order); 1 = Fixed (uses fixed bandwidth value); 2 = Auto (calculates best bandwidth value), Equivalent to the obsolete bandwidth = 0 setting
ziDAQ('set', h, 'sweep/bandwidth', 0); % double  Fixed bandwidth [Hz], 0 = Automatic calculation (obsolete)
ziDAQ('set', h, 'sweep/order', 0); %  int     Defines the filter roll off to use in Fixed bandwidth selection. Valid values are between 1 (6 dB/octave) and 8 (48 dB/octave). An order of 0 triggers a read-out of the order from theselected demodulator.
ziDAQ('set', h, 'sweep/bandwidthoverlap', 0); % Sets the bandwidth overlap mode (default 0).  0 = Disable; 1 = Enable
ziDAQ('set', h, 'sweep/maxbandwidth', 1.25e6);   % double  Maximal bandwidth used in auto bandwidth mode in [Hz]. The default is 1.25MHz.
ziDAQ('set', h, 'sweep/omegasuppression', 40);  %  double  Damping in [dB] of omega and 2omega components. Default is 40dB in favor of sweep speed. Use higher value for strong offset values or 3omega measurement methods.

ziDAQ('set', h, 'sweep/averaging/tc', 0); %  double  Min averaging time [tc]: 0 = no averaging (see also time!); 5 ~ low precision; 15 ~ medium precision; 50 ~ high precision;
ziDAQ('set', h, 'sweep/averaging/sample', 0);% int     Min samples to average: 1 = no averaging (if averaging/tc = 0); 
ziDAQ('set', h, 'sweep/averaging/time', 0); %  double  Min averaging time [s]


ziDAQ('set', h, 'sweep/phaseunwrap', 0);% bool    Enable unwrapping of slowly changing phase evolutions around the +/-180 degree boundary.
ziDAQ('set', h, 'sweep/sincfilter', 0);%  bool    Enables the sinc filter if the sweep frequency s below 50 Hz. This will improve the sweep speed at low frequencies as omega components do not need to be suppressed by the normal low pass filter.
ziDAQ('set', h, 'sweep/awgcontrol', 0);% bool    Enable AWG control for sweeper. If enabled the sweeper will automatically start the AWG and records the sweep sample based on the even index in hwtrigger.




param = ziDAQ('get', h, 'sweep/*');


ziDAQ('subscribe', h, '/dev3571/demods/0/sample');
ziDAQ('execute', h);
while ~ziDAQ('finished', h)
  pause(0.2);
  result = ziDAQ('read', h);
  fprintf('Progress %0.0f%% \t length = %d \n', [ziDAQ('progress', h) * 100 length(result.dev3571.demods(1).sample{1}.r)]);
  if ziCheckPathInData(result, '/dev3571/demods/0/sample')
    plotData(result.dev3571.demods(1).sample{1});
  end
  % Using intermediate reads you can plot an ongoing function.
end

result = ziDAQ('read', h);
if ziCheckPathInData(result, '/dev3571/demods/0/sample')
    plotData(result.dev3571.demods(1).sample{1});
end

ziDAQ('finish', h);
ziDAQ('unsubscribe', h, '*');
ziDAQ('clear', h);


function plotData(sample)
    subplot(2, 1, 1)
    semilogx(sample.grid, sample.r);
    subplot(2, 1, 2)
    semilogx(sample.grid, sample.phase);
end